﻿/*

filipesilvestrim - An open-source ActionScript 3.0 repository of codes for Game Developers 

http://code.google.com/p/filipesilvestrim/

Copyright (c) 2009 Filipe Ghesla Silvestrim, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

package com.filipesilvestrim.manager
{	
	import com.filipesilvestrim.status.state.IState;

	public class AbstractStateChange extends AbstractManager
	{
		// ___________________________________________________________________ CONSTANTS
		
		// ___________________________________________________________________ CLASS PROPERTIES
		
		// ___________________________________________________________________ INSTANCE PROPERTIES
		protected var _current			: IState	= null;
		protected var _previous			: IState	= null;
		protected var _next				: IState	= null;
		
		// ___________________________________________________________________ GETTERS AND SETTERS
		public function get current () 	: IState { return _current; }
		public function get previous () : IState { return _previous; }
		public function get next () 	: IState { return _next; }
		
		// ___________________________________________________________________ CONSTRUCTOR
		
		public function AbstractStateChange () {}
		
		// ___________________________________________________________________ PUBLIC METHODS
		final public function changeTo ( name : * ) : void
		{
			_previous	= _current;
			_next 		= get(name);
			
			if(_previous)
				destroyPrevious();
			buildNext();
		}
		
		final public function kill (name : *, force : Boolean = false) : void
		{
			if (_previous || force)
				internalDestroy();
		}
		
		// ___________________________________________________________________ PRIVATE METHODS
		final protected function destroyPrevious () : void
		{
			if (_previous)
				internalDestroy();
		}
		
		final protected function buildNext () : void
		{
			_current = _next;
			internalBuild();
		}
		
		protected function internalBuild () : void
		{
			_next.build();
		}
		
		protected function internalDestroy () : void
		{
			_current.destroy();
		}
		// ___________________________________________________________________ EVENTS
	}
}

